<template>
  <div class='m-sys_balanceOfProject_goods'>
    <div style='padding-bottom: 20px;'>
      <el-button type='primary' @click='add'>添加商品</el-button>
    </div>
    <a-table :data='data' :cols='tableKeys'>
      <el-table-column align="center" label="操作" width='260' slot='ahead'>
        <template slot-scope="scope">
          <div>
            <el-button size='mini' @click='openEdit(scope.$index)'>修改</el-button>
            <el-button size='mini' @click='deleteObj(scope.row.goods_id)'>删除</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="收支单价值" min-width='100'>
        <template slot-scope="scope">
          <div>
            <span v-show='!scope.row.isEdit'>{{ scope.row.value || '暂无' }}</span>
            <div v-show='scope.row.isEdit'>
              <div class='setValue-box'>
                <el-input v-model='scope.row.cacheValue' />
                <i class='el-icon-check' @click='updatePrice(scope.$index)'></i>
                <i class='el-icon-close' @click='cancelEdit(scope.$index)'></i>
              </div>
            </div>
          </div>
        </template>
      </el-table-column>
    </a-table>
    <div class='u-pagination'>
      <!-- 分页 -->
      <pagination ref='page' @change='getData' :action='fetchData' :pageSize='20'></pagination>
    </div>

    <goods-dialog ref='goodsDialog' @update='search' :id='id' :selectSkuIds='selectSkuIds' />
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import goodsDialog from './components/goodsDialog'

export default {
  name: 'sys_balanceOfProject_goods',

  components: {
    goodsDialog
  },

  data() {
    return {
      tableKeys: [
        { prop: 'name', label: '商品名称', minWidth: '100' }
      ],
      data: [],
      id: '',
      selectSkuIds: [],
      ifRequesting: false
    }
  },

  mounted() {
    const { id } = this.$route.query
    if (!id) {
      return this.$router.replace({ name: 'sys_balanceOfProject' })
    }
    this.id = id
    this.search()
  },

  activated() {
    if (this.isFirst) {
      this.isFirst = false
      return false
    }
    const { id } = this.$route.query
    if (!id) {
      return this.$router.replace({ name: 'sys_balanceOfProject' })
    }
    this.id = id
    this.search()
  },

  methods: {
    ...mapMutations(['setTabRoutes']),

    routerPush(type, args) {
      // push 到缓存里
      this.setTabRoutes({ type: 'add', obj: { ...this.getRouterInfoAccordingName(type), ...args } })
    },

    search() {
      this.$refs.page.changePage()
    },

    async fetchData(aPageNum, aPageSize) {
      return this.$api.inventGetCostGoodses({
        aFirmId: this.aFirmId,
        aCostId: this.id,
        aPageNum,
        aPageSize
      })
    },

    getData(data) {
      this.data = data.map(v => ({
        ...v,
        cacheValue: v.value,
        isEdit: false
      }))
      this.selectSkuIds = data.map(v => v.goods_id)
      console.log(this.selectSkuIds)
    },

    add() {
      this.$refs.goodsDialog.$show()
    },

    set(data) {
      this.$refs.goodsDialog.$setData(JSON.parse(JSON.stringify(data)))
    },

    choose() {
    },

    deleteObj(id) {
      if (this.ifRequesting) return false
      this.__messageBox({
        title: '你确认要删除该商品吗？',
        successCallback: async () => {
          this.ifRequesting = true
          const { code } = await this.$api.inventDeleteCostGoods({ aCostId: this.id, aGoodsId: id })
          this.ifRequesting = false
          this.$refs.page.changePage()
          return code
        }
      })
    },

    async updatePrice(i) {
      const { code } = await this.$api.inventSetCostGoods({
        aFirmId: this.aFirmId,
        aGoodsId: this.data[i].goods_id,
        aValue: this.data[i].value,
        aCostId: this.id
      })
      if (code === 0) {
        this.__notify('提示', '修改成功')
        this.data[i].value = this.data[i].cacheValue
        this.data[i].isEdit = false
      }
    },

    cancelEdit(i) {
      this.data[i].isEdit = false
    },

    openEdit(i) {
      this.data[i].isEdit = true
    }
  }
}
</script>

<style lang='less'>
.m-sys_balanceOfProject_goods {
  .setValue-box {
    display: flex;
    align-items: center;
    i {
      display: block;
    }
  }
}
</style>
